package com.zork.cmdb.webapi.common.exception.handler;

import com.zork.cmdb.webapi.common.exception.CmdbException;
import com.zork.cmdb.webapi.common.exception.CmdbModuleNotFoundException;
import com.zork.cmdb.webapi.common.exception.CmdbRequestLimitException;
import com.zork.cmdb.webapi.common.response.ResponseEntityEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author chenjh
 * @date 2018/5/17
 * @description
 */
@ControllerAdvice
public class CmdbExceptionHandler {

    private Logger logger = LoggerFactory.getLogger(CmdbException.class);

    @ResponseBody()
    @ExceptionHandler(value = CmdbException.class)
    public ResponseEntityEnum exceptionHandler(HttpServletRequest request,CmdbException e) {
        logger.error("error uri:{} , msg:{}", request.getRequestURI(), e.getMessage());
        if (e instanceof CmdbModuleNotFoundException) {
            return ResponseEntityEnum.MODULE_NOT_FOUND_ERROR;
        } else if (e instanceof CmdbRequestLimitException) {
            return ResponseEntityEnum.REJECT;
        } else {
            return ResponseEntityEnum.UNKONW_ERROR.setMsg(e.getMessage());
        }
    }
}
